﻿using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace 转台控制WIN版2024
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private static Mutex? mutex;
        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

            //非UI线程未捕获异常处理事件
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        //兜底未处理异常（对于无await的异步中的异常无法捕获）
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {

            //HandyControl.Controls.MessageBox.Show($"{e.Exception.Message}",
            //        "异常",
            //        MessageBoxButton.OK,
            //        MessageBoxImage.Warning, MessageBoxResult.OK);
            HandyControl.Controls.Growl.Warning(e.Exception.Message);
            e.Handled = true;
        }

        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            // 处理非UI线程中的异常
            e.SetObserved(); // 标记异常已经被处理
                             // 进行异常处理逻辑，例如记录日志、显示错误提示等 
            HandyControl.Controls.Growl.Warning(e.Exception.Message);
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            StringBuilder sbEx = new StringBuilder();
            if (e.IsTerminating)
            {
                sbEx.Append("程序发生致命错误，将终止，请联系运营商！\n");
            }
            sbEx.Append("捕获未处理异常：");
            if (e.ExceptionObject is Exception)
            {
                sbEx.Append(((Exception)e.ExceptionObject).Message);
            }
            else
            {
                sbEx.Append(e.ExceptionObject);
            }
            MessageBox.Show(sbEx.ToString());
        }
        private void App_Startup(object sender, StartupEventArgs e)
        {
            //单例模式
            mutex = new Mutex(true, $"{AppDomain.CurrentDomain.FriendlyName}", out bool ret);
            if (!ret)
            {
                //if (HandyControl.Controls.MessageBox.Show($"{AppDomain.CurrentDomain.FriendlyName}不能重复打开!",
                //    "提示",
                //    MessageBoxButton.OK,
                //    MessageBoxImage.Warning) == MessageBoxResult.OK)
                //{
                //    System.Environment.Exit(0);
                //}



                if (HandyControl.Controls.MessageBox.Show(
                new HandyControl.Data.MessageBoxInfo()
                {
                    Message = $"{AppDomain.CurrentDomain.FriendlyName}不能重复打开!",
                    Caption = "提示",
                    Button = MessageBoxButton.OK,
                    IconBrushKey = ResourceToken.AccentBrush,
                    IconKey = ResourceToken.ErrorGeometry,
                    StyleKey = "MessageBoxCustom1"
                }
                ) == MessageBoxResult.OK)
                {
                    System.Environment.Exit(0);
                }
            }
        }
    }
}
